package cn.yunhe.dplayerdemo3.controller;

import cn.yunhe.dplayerdemo3.Service.*;
import cn.yunhe.dplayerdemo3.entity.*;
import cn.yunhe.dplayerdemo3.util.TransformDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * 显示弹幕
 * 素质三连
 *
 */
@Controller
public class VideoController {

    @Autowired
    private IVideoService videoService;

    @Autowired
    private IDanmakuService danmakuService;

    @Autowired
    private ICoinService coinService;

    @Autowired
    private IGreatService greatService;

    @Autowired
    private ICollectService collectService;

    /**
     *
     * @param video 视频信息
     * @param model 存放数据显示到前台页面
     * @return
     */
    @RequestMapping("/login")
    public String login(Video video, Model model) {
        Video videoByName = videoService.getVideoByName(video.getId());
        model.addAttribute("video",videoByName);

        //显示弹幕
        List<Danmup> list = new ArrayList<Danmup>();
        List<Danmup> danmupList = danmakuService.getDanmu(video.getId());
        for (Danmup d : danmupList) {
            String danmuTime = TransformDate.formatSecondDateTime(Integer.parseInt(d.getTime()));
            System.out.println(danmuTime);
            String text = d.getText();
            String color = d.getColor();
            int position = d.getPosition();
            Danmup danmup = new Danmup(text, color, position, danmuTime);
            list.add(danmup);
        }
        model.addAttribute("danmuList",list);

        //收藏
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(1);
        Collect collect = new Collect();
        collect.setUserInfo(userInfo);
        collect.setVideo(video);

        if (collectService.getCollect(collect) != null) {
            model.addAttribute("msg","取消收藏");
        } else {
            model.addAttribute("msg","收藏");
        }

        //点赞
        Great great = new Great();
        great.setUserInfo(userInfo);
        great.setVideo(video);

        System.out.println(great);
        Great great1 = greatService.queryGreat(great);
        System.out.println(great1);
        if(great1!=null){
            model.addAttribute("great","取消点赞");
        }else{
            model.addAttribute("great","点赞");
        }

        //投币
        Coin coin = new Coin();
        coin.setVideo(video);
        coin.setUserInfo(userInfo);
        if (coinService.getCoin(coin)!=null){
            model.addAttribute("coin","已投币");
        }else{
            model.addAttribute("coin","投币");
        }

        return "test";
    }

}
